﻿Imports NVelocity
Imports NVelocity.App
Imports NVelocity.Context

Imports System.Collections.Generic
Imports System.IO

Imports System.Data
Imports System.Data.SqlClient
Imports NPOI.HSSF.UserModel

Partial Class WebForm_AddTrainingRegister
    Inherits My.MyUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(btnExport)
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(btnExport1)
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(btnPrint)
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(btnUpload)

    End Sub

    '改變場次
    Protected Sub ddlGroupId_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlGroupId.SelectedIndexChanged
        GroupId = ddlGroupId.SelectedValue
        UploadFileControl1.strType = GroupId
        Me.BindIssueDate()
        Me.btnQuery_Click(Nothing, Nothing)
    End Sub

    Protected Sub ddlGroupType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlGroupType.SelectedIndexChanged
        Me.BindddlGroupId()
    End Sub

    Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged
        Me.BindddlGroupId()
    End Sub
    '將發證日期預選場次開始日期 Add by Shihyu 2010/10/12
    Private Sub BindIssueDate()
        StartDate = ""
        StartDate = IIf(Left(Me.ddlGroupId.SelectedItem.Text, 1) = "0", Me.ddlGroupId.SelectedItem.Text.Substring(1, 8), Me.ddlGroupId.SelectedItem.Text.Substring(0, 9))

        Me.txtIssueDate.Text = StartDate
    End Sub
    Private Sub BindYear()

        Dim item As ListItem
        Dim i As Integer = DateTime.Now.Year

        item = New ListItem

        item.Text = "--請選擇--"
        item.Value = ""

        Me.ddlYear.Items.Add(item)


        While i > DateTime.Now.Year - 10

            item = New ListItem
            item.Text = Convert.ToString(i - 1911)
            item.Value = i.ToString()
            Me.ddlYear.Items.Add(item)
            i -= 1

        End While

    End Sub

    Private Sub BindddlGroupId()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        If Me.CanAdmin = False AndAlso DataAccessMode <> EnumDataAccessMode.Edit Then
            sql = New MyOrganizationDataFilter
        End If

        Dim dt As Data.DataTable

        '所有的場次
        sql.AppendFormat("SELECT A.Id, ")
        sql.AppendFormat(" right('0'+CONVERT(varchar,Year(A.StartDate)-1911),3) + '/' +  right('0'+CONVERT(varchar,Month(A.StartDate)),2) + '/' +  right('0'+CONVERT(varchar,Day(A.StartDate)),2) +'  ' + A.GroupName as GroupName, ")
        sql.AppendFormat(" A.UOrganizationID, A.StartDate ")
        sql.AppendFormat("FROM TrainingCourseGroup A ")
        sql.AppendFormat("inner join CodeMapping B on A.GroupType=B.Value and B.TableName ='TrainingCourseGroup' AND B.ColumnName='GroupType' ")
        sql.AppendFormat(" Where 1=1 ")

        If Me.ddlYear.SelectedValue <> "" Then
            sql.AppendFormat(" And Year(A.StartDate) = '{0}' ", Me.ddlYear.SelectedValue)
        End If

        If Me.ddlGroupType.SelectedValue <> "" Then
            sql.AppendFormat(" And A.GroupType = '{0}' ", Me.ddlGroupType.SelectedValue)
        End If

        If Me.CanAdmin = True Then
        Else
            If DataAccessMode = EnumDataAccessMode.Edit Then
                '報名作業:只顯示在報名期間的場次
                sql.AppendFormat("AND getDate() BETWEEN RegStartDate AND dateadd(d,1,RegEndDate) ")
            Else
                'sql.AppendFormat("WHERE getDate() < StartDate ")

                '須小於關帳日才能維護資料
                'sql.AppendFormat("and getdate() > dateadd(d, {0}-day(StartDate) ,dateadd(m,-1,StartDate) ) ", clsAuth.GetCloseDay())
                'sql.AppendFormat("and getdate() < dateadd(d, {0}-day(EndDate) ,dateadd(m,1,EndDate) ) ", clsAuth.GetCloseDay())
                'sql.AppendFormat("and getdate() <= cast(left(convert(char, dateadd(m,1,EndDate), 112),6)+'{0}' as datetime) ", clsAuth.GetCloseDay())
            End If

        End If

        sql.AppendFormat("order by StartDate ")
        'MyObj.WriteLog(sql.ToString())
        dt = dat.GetData(sql)

        ddlGroupId.DataTextField = "GroupName"
        ddlGroupId.DataValueField = "Id"

        ddlGroupId.DataSource = dt
        ddlGroupId.DataBind()

    End Sub

    Protected Sub ddlGroupId_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlGroupId.DataBound
        ddlGroupId.Items.Insert(0, New ListItem(GetLocalResourceObject("PleaseSelect"), "0"))
    End Sub

    Protected Sub chkAllSignIn_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chkAll As CheckBox = TryCast(sender, CheckBox)

        For Each row As GridViewRow In Me.GridView_Volunteers.Rows
            Dim chk As CheckBox = TryCast(row.FindControl("chkSignIn"), CheckBox)

            chk.Checked = chkAll.Checked
        Next

    End Sub

    Protected Sub chkAllSignOut_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chkAll As CheckBox = TryCast(sender, CheckBox)

        For Each row As GridViewRow In Me.GridView_Volunteers.Rows
            Dim chk As CheckBox = TryCast(row.FindControl("chkSignOut"), CheckBox)

            chk.Checked = chkAll.Checked
        Next

    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (ddlGroupId.SelectedIndex <= 0) Then
            MyObj.ShowMessage(GetLocalResourceObject("GroupIdMustInput"))
            Exit Sub
        End If
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Dim i As Integer

        sql.Length = 0

        Using scope As New Transactions.TransactionScope

            For Each gvr As GridViewRow In GridView_Volunteers.Rows

                'Dim cblCheckStatus As CheckBoxList = gvr.FindControl("cblCheckStatus")
                Dim chkSignIn As CheckBox = TryCast(gvr.FindControl("chkSignIn"), CheckBox)
                Dim chkSignOut As CheckBox = TryCast(gvr.FindControl("chkSignOut"), CheckBox)

                i = 0

                If chkSignIn.Checked = True Then
                    i += 1
                End If

                If chkSignOut.Checked = True Then
                    i += 2
                End If

                sql.AppendFormat("UPDATE TrainingRegister SET CheckStatus={0} WHERE Id='{1}';", i, GridView_Volunteers.DataKeys(gvr.RowIndex).Value)

            Next
            '   MyObj.WriteLog(sql.ToString())
            If sql.Length <> 0 Then
                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            End If

            scope.Complete()

        End Using

        MyObj.ShowMessage(Me.GetLocalResourceObject("SaveComplete"))

    End Sub

    Protected Sub btnUploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUploadFile.Click
        If (ddlGroupId.SelectedIndex <= 0) Then
            MyObj.ShowMessage(GetLocalResourceObject("GroupIdMustInput"))
            Exit Sub
        End If
        Me.UploadFileControl1.IsColse = True
        Me.UploadFileControl1.IsAdmin = True
        UploadFileControl1.InitDataAndShow()
    End Sub

#Region "DataBinding-將值分散"

    Protected Sub cblCheckStatus_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cblCheckStatus As CheckBoxList = sender
        Dim gvr As GridViewRow = cblCheckStatus.NamingContainer

        Dim CheckStatus As Integer = DataBinder.Eval(gvr.DataItem, "CheckStatus").ToString()

        If CheckStatus = 1 Then
            cblCheckStatus.Items(0).Selected = True
            cblCheckStatus.Items(1).Selected = False
        ElseIf CheckStatus = 2 Then
            cblCheckStatus.Items(0).Selected = False
            cblCheckStatus.Items(1).Selected = True
        ElseIf CheckStatus = 3 Then
            cblCheckStatus.Items(0).Selected = True
            cblCheckStatus.Items(1).Selected = True
        Else
            cblCheckStatus.Items(0).Selected = False
            cblCheckStatus.Items(1).Selected = False
        End If

    End Sub

#End Region

    Public Function ChangeRegStatus(ByVal strRegStatus As String) As String

        If strRegStatus = "Y" Then
            Return "錄取"
        ElseIf strRegStatus = "B" Then
            Return "備取"
        ElseIf strRegStatus = "N" Then
            Return "取消報名"
        Else
            Return ""
        End If

    End Function

    Public Function ChangeCheckStatus(ByVal strCheckStatus As String) As String

        If strCheckStatus = "Y" Then
            Return "有報到"
        ElseIf strCheckStatus = "T" Then
            Return "臨時報到"
        ElseIf strCheckStatus = "N" Then
            Return "未報到"
        Else
            Return ""
        End If

    End Function

    'Protected Sub ObjectDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles ObjectDataSource1.Selected

    '    Dim drReturnValue As System.Data.DataTable = e.ReturnValue

    '    If FormView_Course.DataItemCount > 0 Then

    '        TryCast(FormView_Course.FindControl("lblCountAll0"), Label).Text = drReturnValue.Rows.Count
    '        TryCast(FormView_Course.FindControl("lblCountA0"), Label).Text = drReturnValue.Select("RegStatus='Y'").Length
    '        TryCast(FormView_Course.FindControl("lblCountB0"), Label).Text = drReturnValue.Select("RegStatus='B'").Length
    '        TryCast(FormView_Course.FindControl("lblCountC0"), Label).Text = drReturnValue.Select("RegStatus='N'").Length

    '    End If

    'End Sub
#Region "匯出訓練課程保險名單"
    Protected Sub btnExport1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport1.Click
        If (ddlGroupId.SelectedIndex <= 0) Then
            MyObj.ShowMessage(GetLocalResourceObject("GroupIdMustInput"))
            Exit Sub
        End If
        Dim ResultDataTable As DataTable
        Dim TrainingGroupTable As DataTable
        Dim dat As New clsData
        Dim sql As New MyStringBuilder


        sql.Length = 0

        sql.AppendFormat(" SELECT    ")
        sql.AppendFormat("  v.PureVolunteerName as VolunteerName ")
        '    sql.AppendFormat(" ,case when A.RegStatus = 'Y' then '' else cm_RegStatus.Text end as RegStatusText  ")
        sql.AppendFormat(" , v.Birthday, v.VolunteerID,  D.VOrganizationName ")

        sql.AppendFormat(" FROM TrainingRegister A  ")
        sql.AppendFormat(" inner join VolunteerDistinctView v on A.VolunteerID = v.VolunteerID And v.Active IN ('2','Y') ")
        sql.AppendFormat(" left join TrainingCourseGroup B on A.GroupId = B.Id  ")
        sql.AppendFormat(" left Join ( Select * From VOrganizationRecord Where VolunteerID IN ")
        sql.AppendFormat(" (Select VolunteerID From TrainingRegister Where GroupID={0}) ", ddlGroupId.SelectedValue)
        sql.AppendFormat(" ) D on A.VolunteerId = D.VolunteerID ")
        ' sql.AppendFormat(" inner join CodeMapping cm_RegStatus on A.RegStatus = cm_RegStatus.Value and cm_RegStatus.TableName = 'TrainingRegister' and cm_RegStatus.ColumnName = 'RegStatus' ")
        If Me.VolunteerQueryControl1.OrganizationID <> "0" OrElse Me.VolunteerQueryControl1.VOrganizationID <> "0" Then
            sql.AppendFormat("  left JOIN (select vm.VOrganizationID,vo.VOrganizationName,vm.VolunteerID ,vo.DirectUOrganizationID from VOrganizationMapping vm ")
            sql.AppendFormat("inner join VOrganizationViewV2 vo on vm.VOrganizationID=vo.VOrganizationID ) oo on A.VolunteerID=oo.VolunteerID  ")
        End If
        sql.AppendFormat(" WHERE (A.GroupId ={0} or {0}=0) ", ddlGroupId.SelectedValue)
        sql.AppendFormat(" and A.RegStatus in ('Y','B') ")

        If Me.VolunteerQueryControl1.OrganizationID <> "0" Then
            sql.AppendFormat("AND (oo.DirectUOrganizationID = '{0}'  ) ", Me.VolunteerQueryControl1.OrganizationID)
        End If
        If Me.VolunteerQueryControl1.VOrganizationID <> "0" Then
            sql.AppendFormat("AND (oo.VOrganizationID = {0} ) ", Me.VolunteerQueryControl1.VOrganizationID)
        End If
        sql.AppendFormat("Order by D.VOrganizationName ")



        ' MyObj.WriteErrorLog(sql.ToString())

        ResultDataTable = dat.GetData(sql)
        If ResultDataTable IsNot Nothing AndAlso ResultDataTable.Rows.Count > 0 Then

            Dim vList As New List(Of TrainingInsurance)
            Dim v As TrainingInsurance

            Dim i As Integer = 0

            For Each dr As DataRow In ResultDataTable.Rows

                v = New TrainingInsurance

                v.sn = i + 1
                ' v.Region = dr("VOrganizationName").ToString().Trim()
                v.VolunteerName = dr("VolunteerName").ToString().Trim()
                v.VolunteerID = dr("VolunteerID").ToString().Trim()
                v.Birthday = dr("Birthday").ToString()
                v.VOrganization = dr("VOrganizationName").ToString().Trim()

                i = i + 1

                vList.Add(v)
            Next

            sql.Length = 0
            sql.AppendFormat("Select A.ClassPlace,A.StartDate,B.OrganizationName ")
            sql.AppendFormat("From TrainingCourseGroup A inner join organizationView B ")
            sql.AppendFormat("On A.UOrganizationID = B.UOrganizationID ")
            sql.AppendFormat("Where A.ID='{0}' ", ddlGroupId.SelectedValue)
            TrainingGroupTable = dat.GetData(sql)

            'Print

            Dim fileName As String = "TrainingInsurance.xls"
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName)
            Response.ContentType = "application/vnd.ms-excel"

            Dim velocity As New VelocityEngine
            Dim props As New Commons.Collections.ExtendedProperties

            'fix bug
            props.AddProperty("file.resource.loader.path", Server.MapPath("~\App_Data"))
            props.AddProperty("input.encoding", "utf-8")
            props.AddProperty("output.encoding", "utf-8")
            velocity.Init(props)

            Dim template As Template

            Dim context As New VelocityContext


            template = velocity.GetTemplate("訓練課程保險名單.xml")

            If TrainingGroupTable.Rows.Count > 0 Then
                context.Put("StartDate", DateTime.Parse(TrainingGroupTable.Rows(0)("StartDate").ToString()).ToString("yyyy/MM/dd"))
                context.Put("OrganizationName", TrainingGroupTable.Rows(0)("OrganizationName").ToString().Trim())
                context.Put("ClassPlace", TrainingGroupTable.Rows(0)("ClassPlace").ToString().Trim())
            End If
            context.Put("User", clsAuth.GetUserID())
            context.Put("DownloadDate", Today.Year.ToString() & "-" & Today.Month.ToString() & "-" & Today.Day.ToString() & "T" & Now.Hour.ToString() & ":" & Now.Minute.ToString() & ":" & Now.Second.ToString() & "Z")
            context.Put("IP", MyObj.getIP())
            context.Put("RowCount", 3 + i)
            context.Put("InsuranceList", vList)

            Dim writer As New IO.StringWriter
            template.Merge(context, writer)

            Response.Write(writer.GetStringBuilder().ToString())

            Response.End()

        Else

            MyObj.ShowMessage(Me.GetLocalResourceObject("NoData"))

        End If

    End Sub
    Public Class TrainingInsurance
        Private OrganizationField As String
        Private ClassPlaceField As String
        Private snField As String
        Private RegionField As String
        Private VolunteerNameField As String
        Private VolunteerIDField As String
        Private BirthdayField As String
        Private VOrganizationField As String

        Public Property Organization() As String
            Get
                Return Me.OrganizationField
            End Get
            Set(ByVal value As String)
                Me.OrganizationField = value
            End Set
        End Property

        Public Property ClassPlace() As String
            Get
                Return Me.ClassPlaceField
            End Get
            Set(ByVal value As String)
                Me.ClassPlaceField = value
            End Set
        End Property

        Public Property sn() As String
            Get
                Return Me.snField
            End Get
            Set(ByVal value As String)
                Me.snField = value
            End Set
        End Property

        Public Property Region() As String
            Get
                Return Me.RegionField
            End Get
            Set(ByVal value As String)
                Me.RegionField = value
            End Set
        End Property

        Public Property VolunteerName() As String
            Get
                Return Me.VolunteerNameField
            End Get
            Set(ByVal value As String)
                Me.VolunteerNameField = value
            End Set
        End Property

        Public Property VolunteerID() As String
            Get
                Return Me.VolunteerIDField
            End Get
            Set(ByVal value As String)
                Me.VolunteerIDField = value
            End Set
        End Property

        Public Property Birthday() As String
            Get
                Return Me.BirthdayField
            End Get
            Set(ByVal value As String)
                Me.BirthdayField = value
            End Set
        End Property

        Public Property VOrganization() As String
            Get
                Return Me.VOrganizationField
            End Get
            Set(ByVal value As String)
                Me.VOrganizationField = value
            End Set
        End Property

    End Class
#End Region

#Region "匯出簽到表"
    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        If (ddlGroupId.SelectedIndex <= 0) Then
            MyObj.ShowMessage(GetLocalResourceObject("GroupIdMustInput"))
            Exit Sub
        End If
        Dim GV As New GridView
        Dim ResultDataTable As DataTable
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim tmpDT As New DataTable

        sql.Length = 0

        sql.AppendFormat(" Select distinct V.VolunteerID ")
        sql.AppendFormat(",V.PureVolunteerName as VolunteerName ")
        sql.AppendFormat(",F.VOrganizationName ")
        sql.AppendFormat(" FROM TrainingRegister A ")
        sql.AppendFormat(" left join VolunteerDistinctView V on A.VolunteerID =V.VolunteerID And V.Active IN ('2','Y') ")
        sql.AppendFormat(" Left join ( Select VolunteerID,VOrganizationName	From VOrganizationRecord Where VolunteerID IN ")
        sql.AppendFormat(" (Select VolunteerID From TrainingRegister Where GroupID='{0}') ", GroupId)
        sql.AppendFormat(" ) F ON A.VolunteerID = F.VolunteerID ")
        If Me.VolunteerQueryControl1.OrganizationID <> "0" OrElse Me.VolunteerQueryControl1.VOrganizationID <> "0" Then
            sql.AppendFormat("  left JOIN (select vm.VOrganizationID,vo.VOrganizationName,vm.VolunteerID ,vo.DirectUOrganizationID from VOrganizationMapping vm ")
            sql.AppendFormat("inner join VOrganizationViewV2 vo on vm.VOrganizationID=vo.VOrganizationID ) oo on F.VolunteerID=oo.VolunteerID  ")
        End If
        sql.AppendFormat(" WHERE (A.GroupId ='{0}' or '{1}'=0) ", ddlGroupId.SelectedValue, ddlGroupId.SelectedValue)
        sql.AppendFormat(" and A.RegStatus in ('Y','B') ")

        If Me.VolunteerQueryControl1.OrganizationID <> "0" Then
            sql.AppendFormat("AND (oo.DirectUOrganizationID = '{0}'  ) ", Me.VolunteerQueryControl1.OrganizationID)
        End If
        If Me.VolunteerQueryControl1.VOrganizationID <> "0" Then
            sql.AppendFormat("AND (oo.VOrganizationID = {0} ) ", Me.VolunteerQueryControl1.VOrganizationID)
        End If
        sql.AppendFormat("Order by F.VOrganizationName ")
        ' MyObj.WriteLog(sql.ToString())
        ResultDataTable = dat.GetData(sql)
        If ResultDataTable IsNot Nothing AndAlso ResultDataTable.Rows.Count > 0 Then
            'Prepare Data

            Dim base As Integer = 16

            Dim vList As New List(Of TrainingRegister)
            Dim v As TrainingRegister



            Dim i As Integer = 1
            Dim prevRegionID As String

            For Each dr As DataRow In ResultDataTable.Rows
                If prevRegionID <> "" AndAlso prevRegionID <> dr("VOrganizationName").ToString() Then

                    For j As Integer = (i Mod base) To base
                        v = New TrainingRegister
                        v.Name = ""
                        v.VillageName = ""
                        v.Index = i
                        v.RegStatus = ""
                        vList.Add(v)

                        i = i + 1
                    Next

                    i = 1
                End If

                v = New TrainingRegister
                v.Name = dr("VolunteerName").ToString()
                v.VillageName = dr("VOrganizationName").ToString()
                '取消備取字樣
                v.RegStatus = ""
                v.Index = i
                vList.Add(v)

                If i Mod base = 1 Then
                    v.IsHeader = True
                Else
                    v.IsHeader = False
                End If

                i += 1
                If Not TypeOf dr("VOrganizationName") Is DBNull Then
                    prevRegionID = dr("VOrganizationName")
                Else
                    prevRegionID = ""
                End If




            Next

            For j As Integer = (i Mod base) To base
                v = New TrainingRegister
                v.Name = ""
                v.VillageName = ""
                v.Index = i
                v.RegStatus = ""
                vList.Add(v)

                i = i + 1
            Next

            'header data
            sql.Length = 0

            sql.AppendFormat("select tcg.StartDate,tcg.GroupType ")
            sql.AppendFormat(", o.OrganizationName ")
            sql.AppendFormat("from TrainingCourseGroup tcg ")
            sql.AppendFormat("inner join OrganizationView o on tcg.UOrganizationID = o.UOrganizationID ")
            sql.AppendFormat("where tcg.ID = {0} ", Me.ddlGroupId.SelectedValue)

            Dim trainingGroupInfo As Data.DataTable = dat.GetData(sql)

            'Print

            Dim fileName As String = "TrainingRegister.xls"
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName)
            Response.ContentType = "application/vnd.ms-excel"

            Dim velocity As New VelocityEngine
            Dim props As New Commons.Collections.ExtendedProperties

            'fix bug
            props.AddProperty("file.resource.loader.path", Server.MapPath("~\App_Data"))
            props.AddProperty("input.encoding", "utf-8")
            props.AddProperty("output.encoding", "utf-8")
            velocity.Init(props)

            Dim template As Template

            Dim context As New VelocityContext

            If trainingGroupInfo.Rows(0)("GroupType") = "B" Then
                context.Put("GroupType", "基礎")
            Else
                context.Put("GroupType", "特殊")

            End If

            If Me.cbExportType.Checked Then
                template = velocity.GetTemplate("基礎訓練簽到表範例.xml")
            Else
                template = velocity.GetTemplate("特殊訓練簽到表範例.xml")
            End If

            context.Put("StartYear", DateTime.Parse(trainingGroupInfo.Rows(0)("StartDate")).Year - 1911)
            context.Put("StartDate", DateTime.Parse(trainingGroupInfo.Rows(0)("StartDate")).ToString("MM/dd"))
            context.Put("OrganizationName", trainingGroupInfo.Rows(0)("OrganizationName").ToString())
            context.Put("PrintRowCount", vList.Count + 2)
            context.Put("VolunteerList", vList)
            context.Put("User", clsAuth.GetUserID())
            context.Put("DownloadDate", Today.Year.ToString() & "-" & Today.Month.ToString() & "-" & Today.Day.ToString() & "T" & Now.Hour.ToString() & ":" & Now.Minute.ToString() & ":" & Now.Second.ToString() & "Z")
            context.Put("IP", MyObj.getIP())
            Dim writer As New IO.StringWriter
            template.Merge(context, writer)

            Response.Write(writer.GetStringBuilder().ToString())

            Response.End()


        Else

            MyObj.ShowMessage(Me.GetLocalResourceObject("NoData"))

        End If

    End Sub

    Public Class TrainingRegister
        Private NameField As String
        Private VillageNameField As String
        Private TownNameField As String
        Private IndexField As Integer
        Private IsHeaderField As Boolean

        Public Property Name() As String
            Get
                Return Me.NameField
            End Get
            Set(ByVal value As String)
                Me.NameField = value
            End Set
        End Property

        Public Property VillageName() As String
            Get
                Return Me.VillageNameField
            End Get
            Set(ByVal value As String)
                Me.VillageNameField = value
            End Set
        End Property

        Public Property TownName() As String
            Get
                Return Me.TownNameField
            End Get
            Set(ByVal value As String)
                Me.TownNameField = value
            End Set
        End Property

        Public WriteOnly Property IsHeader() As Boolean
            Set(ByVal value As Boolean)
                Me.IsHeaderField = value
            End Set
        End Property

        Public ReadOnly Property CellText() As String
            Get
                If Me.IsHeaderField = True Then
                    Return "<Cell ss:MergeDown=""15"" ss:StyleID=""s52""><Data ss:Type=""String""></Data><NamedCell ss:Name=""Print_Area""/></Cell>"
                Else
                    Return ""
                End If
            End Get
        End Property

        Public ReadOnly Property CellText2() As String
            Get
                If Me.IsHeaderField = True Then
                    Return ""
                Else
                    Return "ss:Index=""2"""
                End If
            End Get
        End Property

        Public Property Index() As Integer
            Get
                Return Me.IndexField
            End Get
            Set(ByVal value As Integer)
                Me.IndexField = value
            End Set
        End Property

        Private RegStatusField As String

        Public Property RegStatus() As String
            Get
                Return Me.RegStatusField
            End Get
            Set(ByVal value As String)
                Me.RegStatusField = value
            End Set
        End Property

    End Class
#End Region


    Protected Sub GridView_Volunteers_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView_Volunteers.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim chkSignIn As CheckBox = TryCast(e.Row.FindControl("chkSignIn"), CheckBox)
            Dim chkSignOut As CheckBox = TryCast(e.Row.FindControl("chkSignOut"), CheckBox)

            Dim CheckStatus As Integer = DataBinder.Eval(e.Row.DataItem, "CheckStatus").ToString()

            If CheckStatus = 1 Then
                chkSignIn.Checked = True
                chkSignOut.Checked = False
            ElseIf CheckStatus = 2 Then
                chkSignIn.Checked = False
                chkSignOut.Checked = True
            ElseIf CheckStatus = 3 Then
                chkSignIn.Checked = True
                chkSignOut.Checked = True
            Else
                chkSignIn.Checked = False
                chkSignOut.Checked = False
            End If

            Dim chkAllSignIn As CheckBox = TryCast(Me.GridView_Volunteers.HeaderRow.FindControl("chkAllSignIn"), CheckBox)
            Dim chkAllSignOut As CheckBox = TryCast(Me.GridView_Volunteers.HeaderRow.FindControl("chkAllSignOut"), CheckBox)

            chkAllSignIn.Checked = chkAllSignIn.Checked And chkSignIn.Checked
            chkAllSignOut.Checked = chkAllSignOut.Checked And chkSignOut.Checked
        End If
    End Sub

    Protected Sub GridView_Volunteers_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView_Volunteers.RowUpdated
        Me.FormView_Course.DataBind()
        MyObj.ShowMessage(Me.GetLocalResourceObject("UpdateSuccess"))
    End Sub

    '查詢
    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click

        Dim ds As ObjectDataSource
        '把簽到退審核只能看到錄取名單權限拿掉,可看到錄取及備取 Mod by Shihyu 2011/08/02
        If Me.DataAccessMode = EnumDataAccessMode.Check Then
            ds = ObjectDataSource2
        Else
            ds = ObjectDataSource1
        End If
        ds.SelectParameters("UOrganizationID").DefaultValue = VolunteerQueryControl1.OrganizationID
        'ds.SelectParameters.Item("Region").DefaultValue = VolunteerQueryControl1.Region
        'ds.SelectParameters.Item("Region2").DefaultValue = VolunteerQueryControl1.Region2
        ds.SelectParameters.Item("VOrganizationID").DefaultValue = VolunteerQueryControl1.VOrganizationID
        ds.SelectParameters.Item("VolunteerId").DefaultValue = VolunteerQueryControl1.VolunteerId
        ds.SelectParameters.Item("VolunteerName").DefaultValue = VolunteerQueryControl1.VolunteerName
        ds.SelectParameters.Item("TEL").DefaultValue = VolunteerQueryControl1.VolunteerTEL



        GridView_Volunteers.DataSourceID = ds.ID

        GridView_Volunteers.DataBind()

    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        'If (ddlGroupId.SelectedIndex <= 0) Then
        '    MyObj.ShowMessage(GetLocalResourceObject("GroupIdMustInput"))
        '    Exit Sub
        'End If
        If Me.txtTrainingNo.Text = "" Then
            MyObj.ShowMessage(GetLocalResourceObject("vadTrainingNo.ErrorMessage"))
            Exit Sub
        End If
        Me.PrintTrainingCert()
    End Sub

    Protected Sub btnPageUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope

            For Each MyGridViewRow As GridViewRow In GridView_Volunteers.Rows

                sql.AppendFormat("UPDATE TrainingRegister SET RegStatus='{0}' WHERE Id={1}; ", TryCast(MyGridViewRow.FindControl("RadioButtonList1"), RadioButtonList).SelectedValue, GridView_Volunteers.DataKeys(MyGridViewRow.RowIndex).Value)

            Next

            dat.AccData(sql)

            scope.Complete()

        End Using

        MyObj.ShowMessage(Me.GetLocalResourceObject("SaveComplete"))

        If GridView_Volunteers.PageIndex < GridView_Volunteers.PageCount - 1 Then
            GridView_Volunteers.PageIndex += 1
            GridView_Volunteers.DataBind()
        End If

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        Select Case Me.DataAccessMode

            Case EnumDataAccessMode.Edit
                btnExport.Visible = True
                btnExport1.Visible = True

                Me.GridView_Volunteers.ShowFooter = False
                'If GridView_Volunteers.FooterRow IsNot Nothing Then
                '    MyObj.ShowMessage(GridView_Volunteers.FooterRow.Visible)
                '    GridView_Volunteers.FooterRow.Visible = False
                'End If

                GridView_Volunteers.Columns(4).Visible = True
                GridView_Volunteers.Columns(5).Visible = False
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 1).Visible = True
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 2).Visible = False
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 4).Visible = False
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 5).Visible = False
            Case EnumDataAccessMode.Verify
                btnExport.Visible = True
                btnExport1.Visible = True
                Me.GridView_Volunteers.ShowFooter = True
                GridView_Volunteers.Columns(4).Visible = False
                GridView_Volunteers.Columns(5).Visible = True
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 1).Visible = False
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 2).Visible = True
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 4).Visible = False
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 5).Visible = False
            Case EnumDataAccessMode.Check
                btnUploadFile.Visible = True
                ' btnShow.Visible = False
                btnSave.Visible = True
                Me.btnPrint.Visible = True
                Me.PlaceHolder1.Visible = True
                Me.cbExportType.Visible = False
                btnExport.Visible = False
                btnExport1.Visible = False

                GridView_Volunteers.Columns(4).Visible = True
                GridView_Volunteers.Columns(5).Visible = False
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 1).Visible = False
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 2).Visible = False
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 4).Visible = True
                GridView_Volunteers.Columns(GridView_Volunteers.Columns.Count - 5).Visible = True


        End Select

    End Sub

End Class

Partial Class WebForm_AddTrainingRegister

    Enum EnumDataAccessMode
        Edit
        Verify
        Check
    End Enum

    Public Sub InitData(ByVal intGroup As Integer)

        GroupId = intGroup

        'ddlGroupId
        Me.BindYear()
        Me.BindddlGroupId()
        ddlGroupId.Items.FindByValue(intGroup).Selected = True
        UploadFileControl1.strType = GroupId

        'ValidationGroup
        'chk_ddlGroupId.ValidationGroup = ValidationGroup
        'btnUploadFile.ValidationGroup = ValidationGroup
        'btnExport.ValidationGroup = ValidationGroup
        'btnShow.ValidationGroup = ValidationGroup
        'btnSave.ValidationGroup = ValidationGroup
        'SearchVolunteerControl1.ValidationGroup = ValidationGroup + "SearchVolunteerControl1"

        'PrintValidationGroup
   
        'Me.vadTrainingNo.ValidationGroup = Me.PrintValidationGroup
        'Me.btnPrint.ValidationGroup = Me.PrintValidationGroup
        'Me.vas1.ValidationGroup = Me.PrintValidationGroup

  
        VolunteerQueryControl1.Admin = Me.Admin
        VolunteerQueryControl1.IsVolunteerLeader = IIf(Me.CanAdmin = False And Me.CanQuery = True, True, False)
        VolunteerQueryControl1.InitData()

    End Sub

#Region "Property"

    Public Property DataAccessMode() As EnumDataAccessMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As EnumDataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property


    Public Property GroupId() As Integer
        Get
            If ViewState("GroupId") Is Nothing Then
                Return 0
            End If

            Return ViewState("GroupId")
        End Get
        Set(ByVal value As Integer)
            ViewState("GroupId") = value
        End Set
    End Property

    Public Property ValidationGroup() As String

        Get
            Return ViewState("ValidationGroup")
        End Get
        Set(ByVal value As String)
            ViewState("ValidationGroup") = value
        End Set

    End Property

    Public Property PrintValidationGroup() As String

        Get
            Return ViewState("PrintValidationGroup")
        End Get
        Set(ByVal value As String)
            ViewState("PrintValidationGroup") = value
        End Set

    End Property
#End Region


End Class

Partial Class WebForm_AddTrainingRegister

    '開窗選取志工
    Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
        If (ddlGroupId.SelectedIndex <= 0) Then
            MyObj.ShowMessage(GetLocalResourceObject("GroupIdMustInput"))
            Exit Sub
        End If
        SearchVolunteerControl1.GroupId = GroupId
        SearchVolunteerControl1.InitDataAndShow()

    End Sub

    '選完志工儲存所選志工
    Protected Sub SearchVolunteerControl1_OK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchVolunteerControl1.OK_Click
        Me.SavePerson()
        GridView_Volunteers.DataBind()
    End Sub

    '儲存所選志工
    Private Function SavePerson() As Boolean

        Dim strPersonLimit As String = Convert.ToInt32(TryCast(FormView_Course.FindControl("lblPersonLimit0"), Label).Text)

        If strPersonLimit = String.Empty Then
            strPersonLimit = "0"
        End If

        Dim strCandidate As String = Convert.ToInt32(TryCast(FormView_Course.FindControl("lblCandidate0"), Label).Text)

        If strCandidate = String.Empty Then
            strCandidate = "0"
        End If


        'CreateDate先空白

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope

            ' Dim intCount As Integer = 0 '新流水號

            Dim intAllPerson As Integer = GridView_Volunteers.TotalRowCount
            '只要人數大於正取就要拒絕 備取機制已不存在 mod by Shihyu 2011/08/02
            '(原本人數+此批次人數)>(正取)人數時:拒絕
            If Int32.Parse(intAllPerson + Me.SearchVolunteerControl1.SelectedValues.Count) > (Int32.Parse(strPersonLimit)) Then
                MyObj.ShowMessage(GetLocalResourceObject("OverPersonLimit"))
                Exit Function
            End If

            For Each VolunteerID As String In Me.SearchVolunteerControl1.SelectedValues

                '  intCount = intCount + 1

                Dim strFlag As String = "Y"

                '只要報名就是錄取  此判斷不需要 Mask by Shihyu 2011/08/02
                ''(原本人數+新流水號)<=正取人數時:正取
                'If intAllPerson + intCount <= strPersonLimit Then
                'strFlag = "Y"
                'Else
                '    '(原本人數+新流水號-正取人數)<=備取人數時:備取
                '    If (intAllPerson + intCount - strPersonLimit) <= strCandidate Then
                '        strFlag = "B"
                '    End If
                'End If

                sql.Length = 0
                sql.AppendFormat("INSERT TrainingRegister (GroupId,VolunteerId,RegStatus,CheckStatus,CreateBy,CreateDate) select ")
                sql.AppendFormat("  '{0}' ", GroupId)
                sql.AppendFormat(" ,'{0}' ", VolunteerID)
                sql.AppendFormat(" ,'{0}' ", strFlag)
                sql.AppendFormat(" ,'{0}' ", 0)
                sql.AppendFormat(" ,'{0}' ", Page.User.Identity.Name)
                sql.AppendFormat(" ,getdate() ")

                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            Next
            scope.Complete()
        End Using

        Return True
    End Function

    '上傳志工Excel
    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

        If (ddlGroupId.SelectedIndex <= 0) Then
            MyObj.ShowMessage(GetLocalResourceObject("GroupIdMustInput"))
            Exit Sub
        End If

        If Me.FileUpload1.HasFile Then
            Try

                Dim workbook As New HSSFWorkbook(Me.FileUpload1.FileContent)
                Dim sheet As HSSFSheet = DirectCast(workbook.GetSheetAt(0), HSSFSheet)

                Dim table As New DataTable()
                table.TableName = "table"

                Dim headerRow As HSSFRow = DirectCast(sheet.GetRow(0), HSSFRow)
                Dim cellCount As Integer = headerRow.LastCellNum

                table.Columns.Add("NO")
                table.Columns.Add("IDNO")

                Dim rowCount As Integer = sheet.LastRowNum

                For i As Integer = (sheet.FirstRowNum + 1) To sheet.LastRowNum
                    Dim row As HSSFRow = DirectCast(sheet.GetRow(i), HSSFRow)

                    If row Is Nothing OrElse row.GetCell(0) Is Nothing OrElse row.GetCell(1) Is Nothing Then
                        Exit For
                    End If
                    Dim dataRow As DataRow = table.NewRow()

                    row.GetCell(0).SetCellType(NPOI.SS.UserModel.CellType.STRING)
                    row.GetCell(1).SetCellType(NPOI.SS.UserModel.CellType.STRING)
                 
                    dataRow("NO") = row.GetCell(0).StringCellValue
                    dataRow("IDNO") = row.GetCell(1).StringCellValue



                    table.Rows.Add(dataRow)
                Next

                workbook = Nothing
                sheet = Nothing
  
                Try

                    '存入資料
                    Dim strPersonLimit As String = Convert.ToInt32(TryCast(FormView_Course.FindControl("lblPersonLimit0"), Label).Text)

                    If strPersonLimit = String.Empty Then
                        strPersonLimit = "0"
                    End If

                    Dim strCandidate As String = Convert.ToInt32(TryCast(FormView_Course.FindControl("lblCandidate0"), Label).Text)

                    If strCandidate = String.Empty Then
                        strCandidate = "0"
                    End If

                    Dim dat As New clsData
                    Dim sql As New MyStringBuilder

                    Using scope As New Transactions.TransactionScope

                        'Dim intCount As Integer = 0 '新流水號

                        Dim intAllPerson As Integer = GridView_Volunteers.TotalRowCount
                        '只要人數大於正取就要拒絕 備取機制已不存在 mod by Shihyu 2011/08/02
                        '(原本人數+此批次人數)>(正取)人數時:拒絕
                        If Int32.Parse(intAllPerson + table.Rows.Count) > Int32.Parse(strPersonLimit) Then
                            MyObj.ShowMessage(GetLocalResourceObject("OverPersonLimit"))
                            Exit Sub
                        End If

                        For Each DR_VolunteerID As DataRow In table.Rows

                            sql.Length = 0
                            sql.AppendFormat("SELECT COUNT(*) ")
                            sql.AppendFormat("FROM VolunteerDistinctView A ")
                            sql.AppendFormat("WHERE A.Active IN ('2','Y') ")
                            sql.AppendFormat("and  A.VolunteerID='{0}' ", DR_VolunteerID(1).ToString())

                            Dim strtheCount As String = dat.GetSingleData(sql.ToString(), Nothing, Data.CommandType.Text)

                            If (Convert.ToInt32(strtheCount) = 0) Then
                                MyObj.ShowMessage(String.Format("編號[{0}]的身份證字號不存在於系統內，請先透過「匯入作業」的功能先建立該志工的基本資料!", DR_VolunteerID(0).ToString()))
                                Exit Sub
                            End If

                            '  intCount = intCount + 1

                            Dim strFlag As String = "Y"
                            '只要報名就是錄取  此判斷不需要 Mask by Shihyu 2011/08/02
                            ''(原本人數+新流水號)<=正取人數時:正取
                            'If intAllPerson + intCount <= strPersonLimit Then
                            '    strFlag = "Y"
                            'Else
                            '    '(原本人數+新流水號-正取人數)<=備取人數時:備取
                            '    If (intAllPerson + intCount - strPersonLimit) <= strCandidate Then
                            '        strFlag = "B"
                            '    End If
                            'End If

                            sql.Length = 0
                            sql.AppendFormat("INSERT TrainingRegister (GroupId,VolunteerId,RegStatus,CheckStatus,CreateBy,CreateDate) ")
                            sql.AppendFormat("Select distinct ")
                            sql.AppendFormat("  '{0}' ", GroupId)
                            sql.AppendFormat(" ,A.VolunteerID ")
                            sql.AppendFormat(" ,'{0}' ", strFlag)
                            sql.AppendFormat(" ,'{0}' ", 0)
                            sql.AppendFormat(" ,'{0}' ", Page.User.Identity.Name)
                            sql.AppendFormat(" ,getdate() ")
                            sql.AppendFormat("FROM VolunteerDistinctView A ")
                            sql.AppendFormat("inner join VolunteerStat vs on A.VolunteerID = vs.VolunteerID ")
                            sql.AppendFormat("WHERE A.Active IN ('2','Y') ")
                            sql.AppendFormat("and  A.VolunteerID='{0}' ", DR_VolunteerID(1).ToString())
                            sql.AppendFormat("and  A.VolunteerID not in (SELECT VolunteerId FROM TrainingRegister WHERE GroupId='{0}' and VolunteerId='{1}' ) ", GroupId, DR_VolunteerID(1).ToString())
                            'sql.AppendFormat("AND (A.VolunteerID IN (SELECT distinct VolunteerID FROM VOrganizationMapping WHERE VOrganizationID={0}  ) or '{0}' =0) ", VolunteerQueryControl1.VOrganizationID)

                            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
                        Next
                        scope.Complete()

                        MyObj.ShowMessage("上傳完成。")
                    End Using
                Catch ex As System.Exception
                    MyObj.ShowMessage(GetLocalResourceObject("UploadNG"))
                    Exit Sub
                End Try

                GridView_Volunteers.DataBind()

            Catch ex As System.Exception
                MyObj.ShowMessage(GetLocalResourceObject("FileNG"))
                Exit Sub

            End Try
        Else
            MyObj.ShowMessage("請選擇上傳檔案")
        End If

    End Sub


    Public Property Admin() As Boolean
        Get
            If ViewState("Admin") Is Nothing Then
                Return False
            End If

            Return ViewState("Admin")
        End Get
        Set(ByVal value As Boolean)
            ViewState("Admin") = value
        End Set
    End Property
    Public Property StartDate() As String
        Get

            Return ViewState("StartDate")
        End Get
        Set(ByVal value As String)
            ViewState("StartDate") = value
        End Set
    End Property

#Region "BLL"
    Public Property GroupFilterEnabled() As Boolean
        Get
            If ViewState("GroupFilterEnabled") Is Nothing Then
                Return True
            End If

            Return ViewState("GroupFilterEnabled")
        End Get
        Set(ByVal value As Boolean)
            ViewState("GroupFilterEnabled") = value
        End Set
    End Property


    Private Sub PrintTrainingCert()
        'Prepare Data
        Dim vtList As New List(Of TrainingRecord)
        Dim vt As TrainingRecord

        'vt = New TrainingRecord
        'vt.Name = "張先生"
        'vtList.Add(vt)

        'vt = New TrainingRecord
        'vt.Name = "黃先生"
        'vtList.Add(vt)

        'vt = New TrainingRecord
        'vt.Name = "A先生"
        'vtList.Add(vt)

        vtList.Clear()

        Dim dao As New BLL_AddTrainingRegister
        Dim dt As Data.DataTable = dao.BLL_Select(Me.VolunteerQueryControl1.OrganizationID, Me.ddlGroupId.SelectedValue, _
             Me.VolunteerQueryControl1.VOrganizationID, Me.VolunteerQueryControl1.VolunteerId, Me.VolunteerQueryControl1.VolunteerName, Me.VolunteerQueryControl1.VolunteerTEL)

        '排序  --990526  By Jason
        If Me.GridView_Volunteers.SortExpression <> "" Then
            dt.DefaultView.Sort = Me.GridView_Volunteers.SortSQL
        End If

        '排除未簽到退者
        dt.DefaultView.RowFilter = "CheckStatus='3'"

        Dim i As Integer = 1
        For Each dr As Data.DataRowView In dt.DefaultView
            vt = New TrainingRecord
            vt.Name = dr("PureVolunteerName").ToString()

            vtList.Add(vt)

            i = i + 1
        Next

        'Print

        Dim fileName As String = "training.doc"
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName)

        Dim velocity As New VelocityEngine
        Dim props As New Commons.Collections.ExtendedProperties

        'fix bug
        props.AddProperty("file.resource.loader.path", Server.MapPath("~\App_Data"))
        props.AddProperty("input.encoding", "utf-8")
        props.AddProperty("output.encoding", "utf-8")
        velocity.Init(props)

        Dim template As Template = velocity.GetTemplate("志工訓練證書列印範本v2.xml")
        Dim context As New VelocityContext
        context.Put("IssueYY", DateTime.Parse(Me.txtIssueDate.Text).Year - 1911)
        context.Put("IssueMM", DateTime.Parse(Me.txtIssueDate.Text).ToString("MM"))
        context.Put("IssueDD", DateTime.Parse(Me.txtIssueDate.Text).ToString("dd"))
        context.Put("TrainingNo", Me.txtTrainingNo.Text)
        context.Put("VolunteerTrainingList", vtList)
        context.Put("User", clsAuth.GetUserID())
        context.Put("DownloadDate", Today.Year.ToString() & "-" & Today.Month.ToString() & "-" & Today.Day.ToString() & "T" & Now.Hour.ToString() & ":" & Now.Minute.ToString() & ":" & Now.Second.ToString() & "Z")
        context.Put("IP", MyObj.getIP())
        Dim writer As New IO.StringWriter
        template.Merge(context, writer)

        Response.Write(writer.GetStringBuilder().ToString())

        'Response.WriteFile(Server.MapPath("~\App_Data\志工證.xml"))
        Response.End()
    End Sub

    Public Class TrainingRecord
        Private NameField As String

        Public Property Name() As String
            Get
                Return Me.NameField
            End Get
            Set(ByVal value As String)
                Me.NameField = value
            End Set
        End Property

    End Class
#End Region

End Class

